package com.exam.project.business.service.impl;

import java.util.List;

import com.exam.common.utils.DateUtils;
import com.exam.project.business.domain.Exam;
import com.exam.project.business.mapper.ExamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.exam.project.business.mapper.ExamQuestionMapper;
import com.exam.project.business.domain.ExamQuestion;
import com.exam.project.business.service.IExamQuestionService;

/**
 * 考试关联题目表Service业务层处理
 *
 * @author qiuwenbing
 * @date 2025-04-03
 */
@Service
public class ExamQuestionServiceImpl implements IExamQuestionService {
    @Autowired
    private ExamQuestionMapper examQuestionMapper;
    @Autowired
    private ExamMapper examMapper;

    /**
     * 查询考试关联题目表
     *
     * @param id 考试关联题目表主键
     * @return 考试关联题目表
     */
    @Override
    public ExamQuestion selectExamQuestionById(Long id) {
        return examQuestionMapper.selectExamQuestionById(id);
    }

    /**
     * 查询考试关联题目表列表
     *
     * @param examQuestion 考试关联题目表
     * @return 考试关联题目表
     */
    @Override
    public List<ExamQuestion> selectExamQuestionList(ExamQuestion examQuestion) {
        return examQuestionMapper.selectExamQuestionList(examQuestion);
    }

    /**
     * 新增考试关联题目表
     *
     * @param examQuestion 考试关联题目表
     * @return 结果
     */
    @Override
    public int insertExamQuestion(ExamQuestion examQuestion) {
        if (examQuestion.getQuestionIds() != null) {
            // 删除之前的关联
            examQuestionMapper.deleteExamQuestionByExamId(examQuestion.getExamId());
            examQuestion.getQuestionIds().forEach(questionId -> {
                examQuestion.setCreateTime(DateUtils.getNowDate());
                examQuestion.setQuestionId(questionId);
                examQuestionMapper.insertExamQuestion(examQuestion);
            });
            // 需要计算出来总分
            Exam exam = new Exam();
            exam.setId(examQuestion.getExamId());
            exam.setTotalNum(examQuestion.getQuestionIds().size());
            exam.setTotalScore(examQuestion.getTotalScore());
            examMapper.updateExam(exam);
        }
        return examQuestion.getQuestionIds().isEmpty() ? 0 : 1;
    }

    /**
     * 修改考试关联题目表
     *
     * @param examQuestion 考试关联题目表
     * @return 结果
     */
    @Override
    public int updateExamQuestion(ExamQuestion examQuestion) {
        examQuestion.setUpdateTime(DateUtils.getNowDate());
        return examQuestionMapper.updateExamQuestion(examQuestion);
    }

    /**
     * 批量删除考试关联题目表
     *
     * @param ids 需要删除的考试关联题目表主键
     * @return 结果
     */
    @Override
    public int deleteExamQuestionByIds(Long[] ids) {
        return examQuestionMapper.deleteExamQuestionByIds(ids);
    }

    /**
     * 删除考试关联题目表信息
     *
     * @param id 考试关联题目表主键
     * @return 结果
     */
    @Override
    public int deleteExamQuestionById(Long id) {
        return examQuestionMapper.deleteExamQuestionById(id);
    }
}
